package com.big.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * JSON-String-Object工具类
 */
public class JsonUtils {


    //Object转String
    public static  String  ObjectTurnString(Object object){
        Object data = object;
        //object 转String
        String StringInfo = JSONObject.toJSONString(data);
       return  StringInfo;
    }

    //json(String)转对象
    public static <T> T JSONStringInfoTurnClass(String StringInfo,Class<T> c){
     T t = JSONObject.parseObject(StringInfo, c);
     return  t;

    }

    /**
     * List<T> 转 json 保存到数据库
     */
    public static <T> String listToJson(List<T> ts) {
        String jsons = JSONObject.toJSONString(ts);
        return jsons;
    }

    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }

}
